<?php
class Madmin extends CI_Model {
	function __construct() {
		parent::__construct ();
	}
	
	function getAdminforLogin($username, $pwd) {
		$this->db->select ( 'id,username,authority' );
		$this->db->where ( 'username', $username );
		$this->db->where ( 'password', $pwd );
		$query = $this->db->get ( 'admin' );
		return $query->row_array ();
	}
	
	function checkUsername($username) {
		$this->db->select ( 'id' );
		$this->db->where ( 'username', $username );
		$query = $this->db->get ( 'admin' );
		if ($query->num_rows () > 0) {
			return FALSE;
		} else
			return TRUE;
	}
	
	function addAdmin($username, $pwd) {
		$data = array ('username' => $username, 'password' => $pwd, 'authority' => '2', 'creatTime' => date ( "Y/m/d H:i:s", time () ) );
		$this->db->insert ( 'admin', $data );
		return $this->db->insert_id ();
	}
	
	function checkPwdbyId($pwd, $id) {
		$this->db->select ( 'id' );
		$this->db->where ( 'id', $id );
		$this->db->where ( 'password', $pwd );
		$query = $this->db->get ( 'admin' );
		if ($query->num_rows () > 0) {
			return TRUE;
		} else
			return FALSE;
	}
	
	function modification($pwd, $id) {
		$data ['password'] = $pwd;
		$this->db->where ( 'id', $id );
		$this->db->update ( 'admin', $data );
		if ($this->db->affected_rows () > 0)
			return TRUE;
		else
			return FALSE;
	}
	
	function getAdminNum() {
		return $this->db->count_all_results ( 'admin' );
	}
	
	function getAdminList($page,$num) {
		$this->db->order_by ( "creatTime", "desc" );
		$this->db->limit ( $num, $page );
		$this->db->order_by("id", "asc"); 
		$query = $this->db->get ( 'admin' );
		return $query->result_array ();
	}
	
	function deleteAdmin($id) {
		$this->db->where ( 'id', $id );
		$this->db->delete ( 'admin' );
		if ($this->db->affected_rows () > 0)
			return TRUE;
		else
			return FALSE;
	}
	
	function updateAdmin($data,$id){
		$this->db->where ( 'id', $id );
		$this->db->update ( 'admin', $data );
		if ($this->db->affected_rows () > 0)
			return TRUE;
		else
			return FALSE;
	}
}